package com.hskn.hss.module.userdepart.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.depart.entity.SysDepart;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.module.userdepart.service.ISysUserDepartService;
import com.hskn.hss.utils.GroupingByUtils;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-04-30
 */
@Service
public class SysUserDepartServiceImpl extends ServiceImpl<SysUserDepartMapper, SysUserDepart> implements ISysUserDepartService {
    @Autowired
    SysUserDepartMapper sysUserDepartMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {
        SysUserDepart sysUserDepart = params.getObject("data", SysUserDepart.class);
        sysUserDepart.setId(StringUtils.uuid());
        int i = sysUserDepartMapper.insert(sysUserDepart);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult deleteByid(JSONObject params) throws HssException {
        String id = params.getString("id");
        int i = sysUserDepartMapper.deleteById(id);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public void deleteByUid(String uid) throws HssException {
        Map map = new HashMap();
        map.put("userid", uid);
        sysUserDepartMapper.deleteByMap(map);
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {
        SysUserDepart sysUserDepart = params.getObject("data", SysUserDepart.class);
        int i = sysUserDepartMapper.updateById(sysUserDepart);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult updateByUserid(String uid, String did) throws HssException {

        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        SysUserDepart sysUserDepart = sysUserDepartMapper.selectOne(queryWrapper);
        sysUserDepart.setDepid(did);

        int i = sysUserDepartMapper.updateById(sysUserDepart);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public SysUserDepart getInfoById(JSONObject params) throws HssException {
        String id = params.getString("id");
        SysUserDepart sysUserDepart = sysUserDepartMapper.selectById(id);
        return sysUserDepart;
    }


    @Override
    public String getDeptidByUid(String uid) {
        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        SysUserDepart sysUserDepart = sysUserDepartMapper.selectOne(queryWrapper);
        return sysUserDepart.getDepid();
    }

    @Override
    public List<SysUserDepart> getListByMap(JSONObject params) throws HssException {
        Map map = new HashMap();
        map = params.getObject("queryparams", Map.class);
        List<SysUserDepart> userRolesList = sysUserDepartMapper.selectByMap(map);
        return userRolesList;
    }

    @Override
    public SysDepart getDeptByUid(String uid) throws HssException {
        return sysUserDepartMapper.getDeptByUid(uid);
    }
    @Override
    public Map<String,SysUserDepart> getUserDepart() {
        List<SysUserDepart> userDepartList=sysUserDepartMapper.selectList(new QueryWrapper<>());
        return GroupingByUtils.getGroupingForMapEntity(userDepartList,SysUserDepart::getUserid,
                Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
    }
}
